package com.ling.service;

import com.ling.bean.UserMessage;
import com.ling.bean.UserMessageExample;
import com.ling.dao.UserMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lin
 */
@Service
public class UserMessageService {
    private final UserMessageMapper userMessageMapper;

    @Autowired
    public UserMessageService(UserMessageMapper userMessageMapper) {
        this.userMessageMapper = userMessageMapper;
    }

    //增加消息
    public void saveUserMessage(UserMessage userMessage) {
        userMessageMapper.insertSelective(userMessage);
    }

    //消息已读
    public void userMessageSetReadById(String userMessageId) {
        UserMessage userMessage = new UserMessage();
        userMessage.setId(userMessageId);
        userMessage.setHasread(1);
        userMessageMapper.updateByPrimaryKeySelective(userMessage);
    }

    //消息全部已读
    public void userMessageSetReadByUserId(String userId,int type) {
        UserMessageExample userMessageExample = new UserMessageExample();
        userMessageExample.createCriteria().andUseridEqualTo(userId).andMessagetypeEqualTo(type);
        UserMessage userMessage = new UserMessage();
        userMessage.setHasread(1);
        userMessageMapper.updateByExampleSelective(userMessage,userMessageExample);
    }

    //查询指定用户的所有消息
    public List<UserMessage> getAllUserMessageByUserId(String userId,int type) {
        UserMessageExample userMessageExample = new UserMessageExample();
        userMessageExample.setOrderByClause("hasread ASC");
        userMessageExample.createCriteria().andUseridEqualTo(userId).andMessagetypeEqualTo(type);
        return userMessageMapper.selectByExample(userMessageExample);
    }
}
